package com.bootdo.system.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bootdo.system.domain.DlCompanyAlbumDO;
import com.bootdo.system.service.DlCompanyAlbumService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.system.domain.DlCompanyDO;
import com.bootdo.system.service.DlCompanyService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 
 * 
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2021-06-02 13:39:38
 */
 
@Controller
@RequestMapping("/system/dlCompany")
public class DlCompanyController {
	@Value("${upload.IP}")
	private String IP;
	@Autowired
	private DlCompanyService dlCompanyService;
	@Autowired
	private DlCompanyAlbumService dlCompanyAlbumService;

	@GetMapping()
	@RequiresPermissions("system:dlCompany:dlCompany")
	String DlCompany(){
	    return "system/dlCompany/dlCompany";
	}

	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("system:dlCompany:dlCompany")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<DlCompanyDO> dlCompanyList = dlCompanyService.list(query);
		int total = dlCompanyService.count(query);
		PageUtils pageUtils = new PageUtils(dlCompanyList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("system:dlCompany:add")
	String add(){
	    return "system/dlCompany/add";
	}

	@GetMapping("/edit/{companyId}")
	String edit(@PathVariable("companyId") Integer companyId,Model model, String type){
		DlCompanyDO dlCompany = dlCompanyService.get(companyId);
		HashMap hashMap = new HashMap();
		hashMap.put("companyId", companyId);
		hashMap.put("auditStatus", "3");
		List<DlCompanyAlbumDO> list = dlCompanyAlbumService.list(hashMap);
		for (DlCompanyAlbumDO dlCompanyAlbumDO : list) {
			dlCompanyAlbumDO.setFileName(IP + "/upload/" + dlCompanyAlbumDO.getFileName());
		}
		model.addAttribute("DlCompanyAlbumDOs", list);

		if(dlCompany.getLogo() != null && !dlCompany.getLogo().equals("")){
			dlCompany.setLogo(IP + "/upload/" + dlCompany.getLogo());
		}
		//身份证正反图片路径拼接
		if(dlCompany.getIdentityCardFront() != null && !dlCompany.getIdentityCardFront().equals("")){
			dlCompany.setIdentityCardFront(IP + "/upload/" + dlCompany.getIdentityCardFront());
		}
		if(dlCompany.getIdentityCardContrary() != null && !dlCompany.getIdentityCardContrary().equals("")){
			dlCompany.setIdentityCardContrary(IP + "/upload/" + dlCompany.getIdentityCardContrary());
		}
		if(dlCompany.getIdentityCardHand() != null && !dlCompany.getIdentityCardHand().equals("")){
			dlCompany.setIdentityCardHand(IP + "/upload/" + dlCompany.getIdentityCardHand());
		}
		if(dlCompany.getBusinessLicense() != null && !dlCompany.getBusinessLicense().equals("")){
			dlCompany.setBusinessLicense(IP + "/upload/" + dlCompany.getBusinessLicense());
		}
		model.addAttribute("dlCompany", dlCompany);
		model.addAttribute("type", type);
	    return "system/dlCompany/edit";
	}

	/**
	 * 功能描述: 发送审核失败原因
	 * @Param:
	 * @Return:
	 * @Author: 况鑫威
	 * @Date: 2021/6/3
	 * @Time: 11:25
	 */
	@ResponseBody
	@PostMapping("/auditFailureText")
	public R auditFailureText(String auditFailureText, String id){
		dlCompanyService.auditFailureText(auditFailureText, id);
		return R.ok();
	}
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("system:dlCompany:add")
	public R save( DlCompanyDO dlCompany){
		if(dlCompanyService.save(dlCompany)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	public R update( DlCompanyDO dlCompany, @RequestParam(value = "ids[]",required = false)String[] ids){
		dlCompanyService.update(dlCompany, ids);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("system:dlCompany:remove")
	public R remove( Integer companyId){
		if(dlCompanyService.remove(companyId)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("system:dlCompany:batchRemove")
	public R remove(@RequestParam("ids[]") Integer[] companyIds){
		dlCompanyService.batchRemove(companyIds);
		return R.ok();
	}

}
